package at.socialconference.web.client.ui;

import java.util.List;

import at.socialconference.inter.model.User;
import at.socialconference.web.client.SCwebService;
import at.socialconference.web.client.controller.ViewController;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FriendsHome extends Composite {
		
	private class AsyncCallbackGetFriendRequests implements AsyncCallback<List<User>> {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Could not connect to server");
		}
		@Override
		public void onSuccess(List<User> result) {
			if (result == null || result.size() == 0) {
				return;
			}
			
			rightVerticalPanel = new VerticalPanel();
			rightVerticalPanel.getElement().getStyle().setPaddingLeft(20, Unit.PX);
			
			Label lblFriendRequests = new Label("New friend requests from:");
			lblFriendRequests.setStyleName("textbigger");
			
			rightVerticalPanel.add(lblFriendRequests);
			
			FlowPanel requestingUsers = new FlowPanel();
			rightVerticalPanel.add(requestingUsers);
			
			//"," implemented quick and dirty ;)
			;
			for (User u : result) {
				LabelLinkedUser lblUserRequest = new LabelLinkedUser(u);
				requestingUsers.add(lblUserRequest);
				Label lblColon = new Label(",");
				lblColon.setStyleName("textbig");
				requestingUsers.add(lblColon);
			}
			requestingUsers.remove(requestingUsers.getWidgetCount()-1);
			mainHorizontalPanel.add(rightVerticalPanel);
		}
		
	}
	
	private HorizontalPanel mainHorizontalPanel;
	private VerticalPanel leftVerticalPanel;
	private VerticalPanel rightVerticalPanel;
	private Label lblFriends;
	
	public FriendsHome() {
		mainHorizontalPanel = new HorizontalPanel();
		leftVerticalPanel = new VerticalPanel();
		rightVerticalPanel = new VerticalPanel();
		
		mainHorizontalPanel.add(leftVerticalPanel);
		mainHorizontalPanel.add(rightVerticalPanel);
		
		List<User> friends = ViewController.getInstance().getClientUser().getContacts();
		
		PeopleTable friendsTable = new PeopleTable(false);
		friendsTable.setData(friends);
		
		lblFriends = new Label("Your Friends");
		lblFriends.setStyleName("textbigger");
		
		leftVerticalPanel.add(lblFriends);
		leftVerticalPanel.add(friendsTable);
		
		SCwebService.Util.getInstance().getFriendRequests(new AsyncCallbackGetFriendRequests());
				
		initWidget(mainHorizontalPanel);
	}
}
